//
//  AddServiceAppointmentViewController.m
//  CPICMobileServiceApp
//
//  Created by sinosoft on 13-2-20.
//  Copyright (c) 2013年 lizhiwei. All rights reserved.
//

#import "AddServiceAppointmentViewController.h"
#import "Config.h"
#import "RCAlertViewUtil.h"
#import "ASIHTTPRequest.h"
#import "Config.h"
#import "NSString+SBJson.h"
#import "NSObject+SBJson.h"
#import "AppDelegate.h"
#import "UIImageView+WebCache.h"
#import "IFServiceEvaluation.h"
#import "ASIFormDataRequest.h"
#import <QuartzCore/QuartzCore.h>

#define kPickAppointmentType 0
@interface AddServiceAppointmentViewController ()

@end

@implementation AddServiceAppointmentViewController
@synthesize progress;
@synthesize txtAppointmentName;
@synthesize txtAppointmentTime;
@synthesize txtContactName;
@synthesize txtContactPho;
@synthesize txvNotice;
@synthesize TableViewAppoint;
@synthesize ServiceAppointRefreshdelegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        //self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg640X773@2x"]];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.autoresizesSubviews=TRUE;
    self.view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin
    | UIViewAutoresizingFlexibleRightMargin |  UIViewAutoresizingFlexibleBottomMargin
    | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    
    NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];
    NSString *bgimage= [accountDefaults objectForKey:@"UserDefaultBgKindKey"];
    
    if ([bgimage isEqualToString:@"蓝色主题风格"])
    {
        ADD_BACKGROUND_BUTTONITEM(@"icon640X88@2x.png");
        ADD_LEFT_BUTTONITEM(@"back.png");
        ADD_TITLE_NAMEITEM(@"服务预约新增");
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg640X773@2x"]];
    }
    else if([bgimage isEqualToString:@"棕色主题风格"])
    {
        ADD_BACKGROUND_BUTTONITEM(@"icon640X88_01@2x.png");
        ADD_LEFT_BUTTONITEM(@"back_01.png");
        ADD_TITLE_NAMEITEM(@"服务预约新增");
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg640X773_01@2x"]];
    }
    else if([bgimage isEqualToString:@"重大节日皮肤"])
    {
        NSData *dataImg_02=[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_DATA_02];
        NSLog(@"-------找到%@",dataImg_02);
        NSData *dataImg_03=[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_DATA_03];
        NSData *dataImg_04=[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_DATA_04];
        
        NSURL *urlSkinImg_02=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_URL_02]];
        NSURL *urlSkinImg_03=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_URL_03]];
        NSURL *urlSkinImg_04=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_URL_04]];
        NSLog(@"-------找到333%@",urlSkinImg_02);
        
        UIImageView * LogoImage = [[UIImageView alloc]init];
        [LogoImage setImageWithURL:urlSkinImg_03 success:^(UIImage *image)
         {
             NSData *imgData=UIImagePNGRepresentation(LogoImage.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgData forKey:SKIN_IMG_DATA_03];
         } failure:^(NSError *error)
         {
             [self justShowMessage:@"更新皮肤失败!" title:@"提示"];
         }];
        
        LogoImage.image = [UIImage imageWithData:dataImg_03];
        LogoImage.frame = CGRectMake(0, 0, LogoImage.image.size.width/2, LogoImage.image.size.height/2);
        [self.view addSubview:LogoImage];
        
        //--------------------------
        UIButton * CallOutBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        CallOutBtn1.frame = CGRectMake(10, 10, 109/2, 57/2);
        UIImageView * Back = [[UIImageView alloc]init];
        [Back setImageWithURL:urlSkinImg_02 success:^(UIImage *image)
         {
             NSData *imgData=UIImagePNGRepresentation(Back.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgData forKey:SKIN_IMG_DATA_02];
         } failure:^(NSError *error)
         {
             [self justShowMessage:@"更新皮肤失败!" title:@"提示"];
         }];
        
        [Back setImage:[UIImage imageWithData:dataImg_02]];
        [CallOutBtn1 setImage:Back.image forState:UIControlStateNormal];
        [CallOutBtn1 addTarget:self action:@selector(ClickCallOutBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:CallOutBtn1];
        //--------------------------
        
        
        
        UIImageView *bgimage = [[UIImageView alloc]initWithFrame:CGRectMake(0,44, self.view.frame.size.width, self.view.frame.size.height-44)];
        [bgimage setImageWithURL:urlSkinImg_04 success:^(UIImage *image)
         {
             NSData *imgData=UIImagePNGRepresentation(bgimage.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgData forKey:SKIN_IMG_DATA_04];
         } failure:^(NSError *error)
         {
             [self justShowMessage:@"更新皮肤失败!" title:@"提示"];
         }];
        
        [bgimage setImage:[UIImage imageWithData:dataImg_04]];
        [self.view addSubview:bgimage];
        [bgimage release];
        
        ADD_TITLE_NAMEITEM(@"服务预约新增");
    }
	// Do any additional setup after loading the view.
    arrAppointmentName=[[NSMutableArray alloc]init];
    arrAppointmetnNotice=[[NSMutableArray alloc]init];
    arrAppointMentType=[[NSMutableArray alloc]init];
    arrData=[[NSMutableArray alloc]init];
    
    arrData=[[NSMutableArray alloc]init];
    //添加UITableview
    TableViewAppoint = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 44.0f, self.view.frame.size.width, 250)];
	TableViewAppoint.delegate = self;
	TableViewAppoint.dataSource = self;
    
    TableViewAppoint.opaque = NO;
    TableViewAppoint.backgroundView = nil;
    TableViewAppoint.scrollEnabled=NO;
    TableViewAppoint.autoresizingMask = UIViewContentModeTop;
    TableViewAppoint.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    //[Tableview reloadData];
	[self.view addSubview:TableViewAppoint];
    [TableViewAppoint release];
    

    UILabel *lblNotice=[[UILabel alloc]initWithFrame:CGRectMake(22, 275, 102, 21)];
    lblNotice.backgroundColor=[UIColor clearColor];
    lblNotice.text=@"业务办理须知";
    lblNotice.textColor=[UIColor whiteColor];
    [self.view addSubview:lblNotice];
    
    if (IS_IPHONE_5) {
        txvNotice=[[UITextView alloc]initWithFrame:CGRectMake(20, 315, 280, 180)];
    }
    else
    {
        txvNotice=[[UITextView alloc]initWithFrame:CGRectMake(20, 315, 280, 90)];
    }
    txvNotice.delegate=self;
    txvNotice.returnKeyType=UIReturnKeyDone;
    txvNotice.editable=NO;
    txvNotice.layer.borderColor = [UIColor grayColor].CGColor;
    txvNotice.layer.borderWidth =1.0;
    txvNotice.layer.cornerRadius =5.0;
    [self.view addSubview:txvNotice];
    
    UIButton *btnAdd=[UIButton buttonWithType:UIButtonTypeCustom];
    [btnAdd setTitle:@"提交预约申请" forState:UIControlStateNormal];
    if (IS_IPHONE_5) {
        btnAdd.frame=CGRectMake(86, 505, 310/2, 68/2);
    }
    else
    {
        btnAdd.frame=CGRectMake(86, 415, 310/2, 68/2);
    }
    
    [btnAdd setBackgroundImage:[UIImage imageNamed:@"Submit_Book.png"] forState:UIControlStateNormal];
    [btnAdd addTarget:self action:@selector(subMitAppointment) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnAdd];
    
    NoticeTag=@"1";
    
    NSLog(@"userinfodefault%@",[[NSUserDefaults standardUserDefaults] objectForKey:DICT_USER_INFO]);
    NSDictionary *dictUserInfo=[[NSUserDefaults standardUserDefaults] dictionaryForKey:DICT_USER_INFO];
    if (dictUserInfo!=nil) {
        if ([dictUserInfo valueForKey:@"UserAccount"]) {
            NSLog(@"%@",[dictUserInfo valueForKey:@"UserAccount"]);
        }
    }
    else
    {
        [self justShowMessage:@"用户还没登录,请登录系统" title:@"注意"];
        return;
    }
    [self setRequest];
}
-(void)setAppointmentType
{
    if ([arrAppointmentName count]!=0) {
        CustomActionSheet *sheet=[[CustomActionSheet alloc]initWithHeight:284.0f WithSheetTitle:@"" WithDataArray:arrAppointmentName];
        sheet.tag=0;
        sheet.Customdelegate=self;
        [sheet showInView:self.view];
    }
    else
    {
        
    }

}
-(void)ClickItem:(NSString *)Title selectRow:(NSInteger)SelectRow ActionSheetTag:(NSInteger)actionSheetTag
{
    if (actionSheetTag==0) {
        self.txtAppointmentName.text=Title;
        self.txvNotice.text=[arrAppointmetnNotice objectAtIndex:SelectRow];
        strAppointMentType=[[NSString alloc]initWithFormat:@"%@",[arrAppointMentType objectAtIndex:SelectRow]];
    }
}
-(void) ClickCallOutBtn
{
    [self.ServiceAppointRefreshdelegate RefreshServiceAppointRequest];
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

        static NSString *Identifier=@"Identifier";
        UITableViewCell *cell=(UITableViewCell *)[TableViewAppoint dequeueReusableCellWithIdentifier:Identifier];
        if (cell==nil)
        {
            cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier]autorelease];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;//选中后颜色没变化
        }
    
    if (indexPath.row==0) {
        UILabel *lblAppointmentName=[[UILabel alloc]initWithFrame:CGRectMake(20, 10, 68, 21)];
        lblAppointmentName.backgroundColor=[UIColor clearColor];
        lblAppointmentName.text=@"预约类型";
        //lblAppointmentName.textColor=[UIColor whiteColor];
        [cell addSubview:lblAppointmentName];
        
        txtAppointmentName=[[UITextField alloc]initWithFrame:CGRectMake(105, 10, 161, 31)];
        txtAppointmentName.borderStyle=UITextBorderStyleRoundedRect;
        txtAppointmentName.delegate=self;
        txtAppointmentName.returnKeyType=UIReturnKeyDone;
        txtAppointmentName.enabled=NO;
        [cell addSubview:txtAppointmentName];
        
        UIButton *btnAppointmentName=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        btnAppointmentName.frame=CGRectMake(280, 10, 29, 31);
        [btnAppointmentName addTarget:self action:@selector(setAppointmentType) forControlEvents:UIControlEventTouchDown];
        [cell addSubview:btnAppointmentName];
    }
    else if(indexPath.row==1)
    {
        UILabel *lblAppointmentTime=[[UILabel alloc]initWithFrame:CGRectMake(20, 10, 68, 21)];
        lblAppointmentTime.backgroundColor=[UIColor clearColor];
        lblAppointmentTime.text=@"预约时间";
        //lblAppointmentTime.textColor=[UIColor whiteColor];
        [cell addSubview:lblAppointmentTime];
        
        txtAppointmentTime=[[UITextField alloc]initWithFrame:CGRectMake(105, 10, 161, 31)];
        txtAppointmentTime.borderStyle=UITextBorderStyleRoundedRect;
        txtAppointmentTime.delegate=self;
        txtAppointmentTime.returnKeyType=UIReturnKeyDone;
        txtAppointmentTime.enabled=NO;
        [cell addSubview:txtAppointmentTime];
        
        UIButton *btnAppointmentTime=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        btnAppointmentTime.frame=CGRectMake(280, 10, 29, 31);
        [btnAppointmentTime addTarget:self action:@selector(setAppointmentTime:) forControlEvents:UIControlEventTouchDown];
        [cell addSubview:btnAppointmentTime];
    }
    else if(indexPath.row==2)
    {
        UILabel *lblContact=[[UILabel alloc]initWithFrame:CGRectMake(20, 10, 68, 21)];
        lblContact.backgroundColor=[UIColor clearColor];
        lblContact.text=@"联系人";
        //lblContact.textColor=[UIColor whiteColor];
        [cell addSubview:lblContact];
        
        txtContactName=[[UITextField alloc]initWithFrame:CGRectMake(105, 10, 161, 31)];
        txtContactName.borderStyle=UITextBorderStyleRoundedRect;
        txtContactName.delegate=self;
        txtContactName.returnKeyType=UIReturnKeyDone;
        [cell addSubview:txtContactName];
        
        NSDictionary * dictCustomerInfo =[[NSUserDefaults standardUserDefaults] dictionaryForKey:DICT_CUSTOMER_INFO];
        if (dictCustomerInfo!=nil)
        {
                    if ([dictCustomerInfo valueForKey:@"userName"])
                    {
                        NSLog(@"%@",[dictCustomerInfo valueForKey:@"userName"]);
                        txtContactName.text=[dictCustomerInfo valueForKey:@"userName"];
                    }
                    else
                    {
                    }
        }
        else
        {
        }
    }
    else if(indexPath.row==3)
    {
        UILabel *lblContactNum=[[UILabel alloc]initWithFrame:CGRectMake(20, 10, 161, 31)];
        lblContactNum.backgroundColor=[UIColor clearColor];
        lblContactNum.text=@"联系电话";
        //lblContactNum.textColor=[UIColor whiteColor];
        [cell addSubview:lblContactNum];
        
        txtContactPho=[[UITextField alloc]initWithFrame:CGRectMake(105, 10, 161, 31)];
        txtContactPho.borderStyle=UITextBorderStyleRoundedRect;
        txtContactPho.delegate=self;
        //txtContactPho.returnKeyType=UIReturnKeyDone;
        //txtContactPho.keyboardType=UIKeyboardTypePhonePad;
        CustomNumbleKeyboardView *customKeyboardView = [[CustomNumbleKeyboardView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 238)];
        customKeyboardView.delegate = self;
        [txtContactPho setInputView:customKeyboardView];
        [cell addSubview:txtContactPho];
        NSDictionary * dictCustomerInfo =[[NSUserDefaults standardUserDefaults] dictionaryForKey:DICT_CUSTOMER_INFO];
        if (dictCustomerInfo!=nil)
        {
            if ([dictCustomerInfo valueForKey:@"phone"])
            {
                NSLog(@"%@",[dictCustomerInfo valueForKey:@"phone"]);
                txtContactPho.text=[dictCustomerInfo valueForKey:@"phone"];
            }
            else
            {
            }
        }
        else
        {
        }
    }
    else if(indexPath.row==4)
    {
        UILabel *lblNeedNotice=[[UILabel alloc]initWithFrame:CGRectMake(22, 10, 102, 21)];
        lblNeedNotice.backgroundColor=[UIColor clearColor];
        lblNeedNotice.text=@"是否需要提醒";
        //lblNeedNotice.textColor=[UIColor whiteColor];
        [cell addSubview:lblNeedNotice];
        
        swNeedNotice=[[UISwitch alloc]initWithFrame:CGRectMake(160, 10, 100, 21)];
        [swNeedNotice setOn:YES];
        [swNeedNotice addTarget:self action:@selector(setNoticeTag) forControlEvents:UIControlEventValueChanged];
        [cell addSubview:swNeedNotice];
    }
    


    return cell;
     
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
//数字键盘
- (void)numberKeyBoardInput:(NSInteger) number
{
    NSMutableString *textString = [[[NSMutableString alloc] initWithFormat:@"%@%d",txtContactPho.text,number] autorelease];
    if ([textString length]>11)
    {
        [self justShowMessage:@"手机号码输入不能超过11位!" title:@""];
        return;
    }
    txtContactPho.text = textString;
}

- (void)numberKeyBoardBackspace
{
    NSMutableString* mutableString = [[[NSMutableString alloc] initWithFormat:@"%@", txtContactPho.text] autorelease];
    if ([mutableString length] >=1) {
        NSRange tmpRange;
        tmpRange.location = [mutableString length] - 1;
        tmpRange.length = 1;
        [mutableString deleteCharactersInRange:tmpRange];
    }
    txtContactPho.text = mutableString;
    
}

- (void)numberKeyBoardFinish
{
    [txtContactPho resignFirstResponder];
}

-(void)subMitAppointment
{
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    myDelegate.bServiceAppointment=TRUE;
    
    if (txtAppointmentName.text.length==0) {
        [self justShowMessage:@"预约类型不能为空" title:@"注意"];
        return;
    }
    if (txtAppointmentTime.text.length==0) {
        [self justShowMessage:@"预约时间不能为空" title:@"注意"];
        return;
    }
    if (txtContactName.text.length==0) {
        [self justShowMessage:@"联系人不能为空" title:@"注意"];
        return;
    }
    if (txtContactPho.text.length==0) {
        [self justShowMessage:@"联系电话不能为空" title:@"注意"];
        return;
    }
    [self setAppointRequest];
}
-(void)setAppointRequest
{
    NSLog(@"userinfodefault%@",[[NSUserDefaults standardUserDefaults] objectForKey:DICT_USER_INFO]);
    NSDictionary *dictUserInfo=[[NSUserDefaults standardUserDefaults] dictionaryForKey:DICT_USER_INFO];
    if (dictUserInfo!=nil) {
        if ([dictUserInfo valueForKey:@"UserAccount"]) {
            NSLog(@"%@",[dictUserInfo valueForKey:@"UserAccount"]);
        }
    }
    else
    {
        [self justShowMessage:@"用户还没登录,请登录系统" title:@"注意"];
        return;
    }
    
    
//    NSString *strRequestCode=@"RequestCode=30601";
    NSMutableDictionary *dicRequest = [NSMutableDictionary dictionary];
    if (strAppointMentType.length!=0) {
        [dicRequest setValue:strAppointMentType forKey:@"appointmentType"];
    }
    else
    {
        [dicRequest setValue:@"" forKey:@"appointmentType"];
    }
    
    if (self.txtAppointmentTime.text.length!=0) {
        [dicRequest setValue:txtAppointmentTime.text forKey:@"appointmentTime"];
    }
    else
    {
        [dicRequest setValue:@"" forKey:@"appointmentTime"];
    }
    
    if (self.txtContactName.text.length!=0) {
        [dicRequest setValue:txtContactName.text forKey:@"contactName"];
    }
    else
    {
        [dicRequest setValue:@"" forKey:@"contactName"];
    }
    
    if (self.txtContactPho.text.length!=0) {
        [dicRequest setValue:txtContactPho.text forKey:@"contactPhone"];
    }
    else
    {
        [dicRequest setValue:@"" forKey:@"contactPhone"];
    }
    
    if (NoticeTag.length!=0) {
        [dicRequest setValue:NoticeTag forKey:@"notice"];
    }
    else
    {
        [dicRequest setValue:@"" forKey:@"notice"];
    }
    [dicRequest setValue:[dictUserInfo valueForKey:@"UserAccount"] forKey:@"userAccount"];

    NSString *strRequestJson=[dicRequest JSONRepresentation];
    //strRequestJson=[NSString stringWithFormat:@"&RequestData=%@",strRequestJson];
//    NSString *strUrl=[NSString stringWithFormat:@"%@%@%@",Server_Url,strRequestCode,strRequestJson];
//    NSString *strUrlEncod = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//字符串编码转换
//    NSURL *myurl = [NSURL URLWithString:strUrlEncod];//This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’. Note that ‘%’ escapes are translated via UTF-8.
//    NSLog(@"请求报文为:%@",strUrl);
    ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:Server_Url_Form]];
    [request setPostValue:@"30601" forKey:@"RequestCode"];
    [request setPostValue:strRequestJson forKey:@"RequestData"];
    
    [request setDelegate:self];
    [request setDidFailSelector:@selector(ResponseAppointFail:)];
    [request setDidFinishSelector:@selector(ResonseAppointSuccess:)];
    [request startAsynchronous];
    
    showProgress = YES;
    if (showProgress) {
        NSLog(@"88888");
        // [NSThread sleepForTimeInterval:1];
        progress= [[MBProgressHUD alloc]initWithView:self.view];
        [self.view addSubview:progress];
        [self.view bringSubviewToFront:progress];
        progress.labelText = @"查询中...";
        [progress show:YES];
        showProgress = NO;
    }
}

- (void)ResonseAppointSuccess:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSString *responseString = [request responseString];
    NSLog(@"responseString %@",responseString);
    NSMutableDictionary *dictResponse=[responseString JSONValue];
    NSLog(@"dictResponse  %@",dictResponse);
    NSMutableDictionary *dictHead=[dictResponse valueForKey:@"head"];
    NSString *strResult= [[NSString alloc] initWithFormat:@"%@",[dictHead objectForKey:@"success"]];
    NSLog(@"strResult%@",strResult);
    if ([strResult isEqualToString:@"1"]) {
        //[self justShowMessage:@"提交成功" title:nil];
        
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"提交成功！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alert show];
            alert.delegate = self;
            [alert release];
            return;
        
    }
    else
    {
        [self justShowMessage:@"提交失败" title:nil];
    }
    [strResult release];
    
    
}
- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch(alert.tag)
    {
        case 0:
            if (buttonIndex == [alert cancelButtonIndex])
                //默认cancelButtonIndex = 0，每个按钮index可设
                [self.ServiceAppointRefreshdelegate RefreshServiceAppointRequest];
                [self.navigationController popViewControllerAnimated:YES];
            {
            }
            break;
        case 1:
            return;
        default:
            break;
    }
}


- (void)ResponseAppointFail:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSLog(@"服务器连接失败!");
    [self justShowMessage:@"服务器连接失败" title:@"注意"];
}


-(void)setNoticeTag
{
    if([swNeedNotice isOn])
    {
        NoticeTag=@"1";
    }
    else
    {
        NoticeTag=@"0";
    }
}

-(void)setAppointmentTime:(id)sender
{
    CustomDateActionSheet *dateActionSheet=[[CustomDateActionSheet alloc]initWithHeight:284.0f WithSheetTitle:nil];
    dateActionSheet.tag=0;
    dateActionSheet.CustomDateDelegate=self;
    [dateActionSheet showInView:self.view];
    
}

-(void)ClickDate:(NSString *)Date DateActionSheetTag:(NSInteger)dateActionSheetTag
{
    if (dateActionSheetTag==0) {
        self.txtAppointmentTime.font=[UIFont systemFontOfSize:14.0];
        self.txtAppointmentTime.text=Date;
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.

}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if(textField==txtContactName)
    {
        [self SetAnimation:30];
    }
    else if(textField==txtContactPho)
    {
        [self SetAnimation:80];
    }
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if(textField==txtContactName)
    {
        [self backAnimation:30];
    }
    else if(textField==txtContactPho)
    {
        [self backAnimation:80];
    }
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(textField==txtAppointmentName)
    {
        if(range.location>=20)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    else if(textField==txtAppointmentTime)
    {
        if(range.location>=20)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    else if(textField==txtContactName)
    {
        if(range.location>=20)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    else if(textField==txtContactPho)
    {
        if(range.location>=20)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
-(void)SetAnimation:(CGFloat)Xorigin
{
    NSTimeInterval animationDuration = 0.30f;
    CGRect frame = self.view.frame;
    //frame.origin.x+=Xorigin;
    frame.origin.y-=Xorigin;
    //frame.size.height +=Xorigin;
    self.view.frame = frame;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame = frame;
    [UIView commitAnimations];
}

-(void)backAnimation:(CGFloat)Xorigin
{
    NSTimeInterval animationDuration = 0.30f;
    CGRect frame = self.view.frame;
    //frame.origin.x -=Xorigin;
    frame.origin.y+=Xorigin;
    //frame.size.height -=Xorigin;
    self.view.frame = frame;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame = frame;
    [UIView commitAnimations];
    // [textField resignFirstResponder];
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if(textView==txvNotice)
    {
        [self SetAnimation:180];
    }
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if(textView==txvNotice)
    {
        [self backAnimation:180];
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
    if ([@"\n" isEqualToString:text] == YES)
    {
        [textView resignFirstResponder];
        return NO;
    }
    
    return YES;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)setRequest//向太保移动平台发送请求报文
{
//    NSString *strRequestCode=@"RequestCode=30501";
    NSMutableDictionary *dicRequest = [NSMutableDictionary dictionary];
//    NSNumber *PageNo=[NSNumber numberWithInt:1];
//    [dicRequest setValue:PageNo forKey:@"pageNo"];
//    NSNumber *PageSize=[NSNumber numberWithInt:10];
//    [dicRequest setValue:PageSize forKey:@"pageSize"];
    [dicRequest setObject:@"" forKey:@"userAccount"];
    NSString *strRequestJson=[dicRequest JSONRepresentation];
//    strRequestJson=[NSString stringWithFormat:@"&RequestData=%@",strRequestJson];
//    NSString *strUrl=[NSString stringWithFormat:@"%@%@%@",Server_Url,strRequestCode,strRequestJson];
//    NSString *strUrlEncod = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//字符串编码转换
//    NSURL *myurl = [NSURL URLWithString:strUrlEncod];//This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’. Note that ‘%’ escapes are translated via UTF-8.
//    NSLog(@"请求报文为:%@",strUrl);
    ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:Server_Url_Form]];
    
    //设置需要POST的数据，这里提交两个数据，A=a&B=b
    [request setPostValue:@"30501" forKey:@"RequestCode"];
    [request setPostValue:strRequestJson forKey:@"RequestData"];
    
    [request setDelegate:self];
    [request setDidFailSelector:@selector(ResponseFail:)];
    [request setDidFinishSelector:@selector(ResonseSuccess:)];
    [request startAsynchronous];
    
    showProgress = YES;
    if (showProgress) {
        NSLog(@"88888");
        // [NSThread sleepForTimeInterval:1];
        progress= [[MBProgressHUD alloc]initWithView:self.view];
        [self.view addSubview:progress];
        [self.view bringSubviewToFront:progress];
        progress.labelText = @"查询中...";
        [progress show:YES];
        showProgress = NO;
    }
}

- (void)ResonseSuccess:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSString *responseString = [request responseString];
    NSLog(@"responseString %@",responseString);
    NSMutableDictionary *dictResponse=[responseString JSONValue];
    NSLog(@"dictResponse  %@",dictResponse);
    NSMutableDictionary *dictAppointmentSettingList=[dictResponse valueForKey:@"appointmentTypeList"];
    arrData=[dictAppointmentSettingList valueForKey:@"data"];
    for (int i=0; i<[arrData count];i++) {
        if ([[arrData objectAtIndex:i]valueForKey:@"appointmentTypeName"]) {
            [arrAppointmentName addObject:[[arrData objectAtIndex:i]valueForKey:@"appointmentTypeName"]];
        }
        if ([[arrData objectAtIndex:i]valueForKey:@"workGuide"]) {
            [arrAppointmetnNotice addObject:[[arrData objectAtIndex:i]valueForKey:@"workGuide"]];
        }
        if ([[arrData objectAtIndex:i]valueForKey:@"appointmentType"]) {
            [arrAppointMentType addObject:[[arrData objectAtIndex:i]valueForKey:@"appointmentType"]];
        }
    }
}

- (void)ResponseFail:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSLog(@"服务器连接失败!");
    [self justShowMessage:@"服务器连接失败" title:@"注意"];
}


-(void)dealloc
{
    [arrData release];
    [arrAppointmetnNotice release];
    [arrAppointmentName release];
    [arrAppointMentType release];
    [super dealloc];
}

@end
